package com.aibaixun.system.service.impl;

import com.aibaixun.common.constant.Constants;
import com.aibaixun.common.core.domain.R;
import com.aibaixun.common.exception.ServiceException;
import com.aibaixun.common.utils.BeanCopyUtils;
import com.aibaixun.common.utils.IdCardUtils;
import com.aibaixun.common.utils.StringUtils;
import com.aibaixun.system.domain.WorkOrderUser;
import com.aibaixun.system.domain.dto.DicmWorkOrderDto;
import com.aibaixun.system.service.EvsApiService;
import com.aibaixun.system.service.RequestLogService;
import com.aibaixun.system.service.WorkOrderUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aibaixun.system.domain.DicmWorkOrder;
import com.aibaixun.system.service.DicmWorkOrderService;
import com.aibaixun.system.mapper.DicmWorkOrderMapper;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
* @author zhangyh
* @description 针对表【dicm_work_order(工单信息表)】的数据库操作Service实现
* @createDate 2023-09-14 11:19:40
*/
@Service
public class DicmWorkOrderServiceImpl extends ServiceImpl<DicmWorkOrderMapper, DicmWorkOrder>
    implements DicmWorkOrderService{

    @Autowired
    private WorkOrderUserService workOrderUserService;

    @Autowired
    @Lazy
    private EvsApiService evsApiService;

    @Override
    public R workOrderPush(DicmWorkOrderDto dto) {
        if(StringUtils.isBlank(dto.getBadgeNum()) && StringUtils.isBlank(dto.getResidentId())){
            throw new ServiceException("身份证号和工作证号必须填写一个！");
        }
        dto.setGender(StringUtils.equals("男",dto.getGender()) ? "male" : "female");
        DicmWorkOrder dicmWorkOrder = new DicmWorkOrder();
        BeanCopyUtils.copy(dto,dicmWorkOrder);
        dicmWorkOrder.setCreatedTime(LocalDateTime.now());
        if(StringUtils.isBlank(dicmWorkOrder.getResidentId())){
            dicmWorkOrder.setResidentId(IdCardUtils.generateID());
        }
        this.baseMapper.insert(dicmWorkOrder);

        WorkOrderUser user = workOrderUserService.getBaseMapper().selectOne(
            new LambdaQueryWrapper<WorkOrderUser>()
            .eq(WorkOrderUser::getPersonName,dto.getPersonName())
            .eq(WorkOrderUser::getGender,dto.getGender())
            .eq(StringUtils.isBlank(dto.getResidentId()),WorkOrderUser::getBadgeNum,dto.getBadgeNum())
            .eq(StringUtils.isBlank(dto.getBadgeNum()),WorkOrderUser::getResidentId,dto.getResidentId())
            .last("limit 1")
        );
        if(user == null){
            //人员不存在，需要新增本地人员库并且上传到EVS
            user = new WorkOrderUser();
            user.setGender(dicmWorkOrder.getGender());
            user.setPersonName(dicmWorkOrder.getPersonName());
            user.setPersonPhone(dicmWorkOrder.getPersonPhone());
            user.setResidentId(dicmWorkOrder.getResidentId());
            user.setBadgeNum(dicmWorkOrder.getBadgeNum());
            user.setStatus(Constants.INT_0);
            user.setPersonPhoto(dto.getPersonPhoto());
            workOrderUserService.getBaseMapper().insert(user);
            String thirdId = evsApiService.addEvsMember(user);
            if(StringUtils.isNotBlank(thirdId)){
                user.setThirdUserId(thirdId);
                user.setStatus(Constants.INT_1);
                workOrderUserService.getBaseMapper().updateById(user);
            }
        }else{
            if(user.getStatus().intValue() != Constants.INT_1){
                String thirdId = evsApiService.addEvsMember(user);
                if(StringUtils.isNotBlank(thirdId)){
                    user.setThirdUserId(thirdId);
                    user.setStatus(Constants.INT_1);
                    user.setPersonPhoto(dto.getPersonPhoto());
                    workOrderUserService.getBaseMapper().updateById(user);
                }
            }
        }
        //更新工单关联用户信息
        dicmWorkOrder.setUserId(user.getUserId());
        this.baseMapper.updateById(dicmWorkOrder);
        return R.ok();
    }
}




